//
//  CPBaseTableViewModel.m
//  
//  jhf
//
//  Created by 计海峰 on 2019/6/5.
//  Copyright © 2019 计海峰. All rights reserved.
//

#import "CPBaseTableViewModel.h"
#import <MJRefresh/MJRefresh.h>
#import <CooperSDK/CooperSDK.h>
@interface CPBaseTableViewModel ()

@property (nonatomic, strong) NSIndexPath *lpGRIndexPath;

@property (nonatomic, copy) LongPressBlock presBlock;

@end

@implementation CPBaseTableViewModel

#pragma mark - 设置 长按
- (void)configLongPress:(UITableView *)tableview completion:(LongPressBlock)completionHandler;{
    self.presBlock = completionHandler;
    UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpGR:)];
    longPressGR.minimumPressDuration = 0.7;
    [tableview addGestureRecognizer:longPressGR];
}

- (void)lpGR:(UILongPressGestureRecognizer *)lpGR {
    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
        UITableView *tableview = (UITableView *)lpGR.view;
        CGPoint point = [lpGR locationInView:tableview];
        NSIndexPath *currentIndexPath = [tableview indexPathForRowAtPoint:point]; // 可以获取我们在哪个cell上长按
        self.lpGRIndexPath = currentIndexPath;
        self.presBlock(self.lpGRIndexPath, UIGestureRecognizerStateBegan);
    }else if (lpGR.state == UIGestureRecognizerStateEnded) { //手势结束
        if (self.presBlock) {
            self.presBlock(self.lpGRIndexPath, UIGestureRecognizerStateEnded);
        }
    }
}

#pragma mark - 设置header footer
- (void)configHeader:(UITableView *)tableview completion:(void (^ __nullable)(id __nullable result)) completionHandler {
    @weakify(self)
    @weakify(tableview)
    tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        [self refreshData:^(id _Nonnull result) {
            @strongify(tableview)
            [tableview.mj_header endRefreshing];
            if (completionHandler) {
                completionHandler(result);
            }
        }];
    }];
}

- (void)checkFooter:(UITableView *)tableview result:(id)result pagesize:(int)pagesize {
    pagesize = pagesize >= 1 ? pagesize : 5;
    if (result && [result isKindOfClass:[NSArray class]]) {
        if ([(NSArray *)result count] > 0) {
            [self configFooter:tableview completion:^(id  _Nullable result) { }];
            return;
        }
        if ([(NSArray *)result count] > 0 && [(NSArray *)result count] % pagesize == 0) {
//            @weakify(self)
            [self configFooter:tableview completion:^(id  _Nullable result) {
//                @strongify(self)
//                [self checkFooter:tableview result:result pagesize:pagesize];
            }];
        }else {
            [self cancelFooter:tableview];
        }
    }else {
        [self cancelFooter:tableview];
    }
}

- (void)configFooter:(UITableView *)tableview completion:(void (^ __nullable)(id __nullable result))completionHandler {
    @weakify(self)
    @weakify(tableview)
    tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        [self pullData:^(id  _Nonnull result) {
            @strongify(tableview)
            [tableview.mj_footer endRefreshing];
            if (completionHandler) {
                completionHandler(result);
            }
        }];
    }];
}

- (void)cancelFooter:(UITableView *)tableview; {
    tableview.mj_footer = nil;
}

#pragma mark - 索引
- (NSArray<NSString *> *)sectionIndexTitles:(UITableView *)tableview; {
    return @[];
}

- (NSInteger)selectSectionIndexTitles:(UITableView *)tableview title:(NSString *)title index:(NSInteger)index; {
    return index;
}

#pragma mark - 更新数据
- (void)refreshData:(void (^ __nullable)(id __nullable result))completionHandler {
    if (completionHandler) {
        completionHandler(nil);
    }
}

- (void)forData:(void (^)(id _Nullable))completionHandler {
    
}

- (void)pullData:(void (^ __nullable)(id __nullable result))completionHandler ;{
    if (completionHandler) {
        completionHandler(nil);
    }
}

#pragma mark - 刷新部分
- (void)reloadSections:(NSIndexSet *)sections tableview:(UITableView *)tableview; {
    [UIView setAnimationsEnabled:false];
    [tableview reloadSections:sections withRowAnimation:UITableViewRowAnimationNone];
    [UIView setAnimationsEnabled:true];
}

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths tableview:(UITableView *)tableview; {
    [UIView setAnimationsEnabled:false];
    [tableview reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [UIView setAnimationsEnabled:true];
}

#pragma mark - delegate and datasource
- (NSInteger)sectionsCount{
    return 1;
}

- (NSInteger)sectionCellCount:(NSInteger)section tableView:(UITableView *)tableView {
    return 1;
}

- (UIView *)sectionHeaderView:(NSInteger)section tableView:(UITableView *)tableView {
    return [UIView new];
}

- (CGFloat)sectionHeaderHeight:(NSInteger)section tableView:(UITableView *)tableView {
    return 0.01f;
}

- (UIView *)sectionFooterView:(NSInteger)section tableView:(UITableView *)tableView; {
    return [UIView new];
}

- (CGFloat)sectionFooterHeight:(NSInteger)section tableView:(UITableView *)tableView; {
    return 0.01f;
}

- (UITableViewCell *)cell:(NSIndexPath *)indexPath tableView:(UITableView *)tableView; {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableViewCell *)defaultCell:(NSIndexPath *)indexPath tableView:(UITableView *)tableView reusableIdentier:(NSString *)identifier; {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)cellHeight:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
    return UITableViewAutomaticDimension;
}


- (NSArray<UITableViewRowAction *> *)rowsAction:(NSIndexPath *)indexPath tableView:(UITableView *)tableView; {
    return nil;
}

- (void)select:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
//    Section *sec = [self.sections objectAtIndex:indexPath.section];
//    Row *row = [sec.rows objectAtIndex:indexPath.row];
}

- (void)willDisplayCell:(NSIndexPath *)indexPath tableView:(UITableView *)tableView cell:(UITableViewCell *)cell {
    
}

- (void)didScroll:(UIScrollView *)scrollView; {
    
}

- (void)scrollTo:(UIScrollView *)scrollView indexPath:(NSIndexPath *)indexPath {
    
}

// 侧滑
- (NSArray<UITableViewRowAction *> *)rowActionIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
    return @[];
}

- (CPEmptyDataConfig *)emptyDataConfig; {
    return [CPEmptyDataConfig initWithTitle:@"暂无数据" imageName:CPDEFAULTEMPTYIMAGE];
}

@end
